package com.yicong.ants.ui.me;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.google.gson.Gson;
import com.yicong.ants.R;
import com.yicong.ants.databinding.ActivityCancellationBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.ConstantCodeUtils;

import io.reactivex.disposables.Disposable;

/**
 * 注销账号
 */
public class CancellationActivity extends BaseTitleBarActivity<ActivityCancellationBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.activity_cancellation;
    }

    @Override
    protected void initEventAndData() {
        AppHelper.transStatus(getWindow());
        setTitleText("注销账号");
        setTitleBarVisible(false);
        mDataBind.setClick(this);
    }

    @Override
    protected void onLoadData() {

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back) {
            onBackPressed();
        } else if (id == R.id.next) {
            statusConfirmation();
        } else if (id == R.id.check_group) {
            mDataBind.checkBox.setChecked(!mDataBind.checkBox.isChecked());
            mDataBind.next.setClickable(mDataBind.checkBox.isChecked());
            mDataBind.next.setEnabled(mDataBind.checkBox.isChecked());
        }
    }

    //确认状态
    private void statusConfirmation() {
        showProgress();
        Disposable disposable = RetrofitHelper.getApis().checkDestroyAccount()
                .compose(RxHelper.toMain())
                .subscribe(respBean -> {
                    hideProgress();
                    if (respBean.getData().getValidRose().size()>0 || respBean.getData().getTradeRefundIng().size()>0){
                        Router.turnTo(mContext, CancellationStatusConfirmationActivity.class).putExtra("data", new Gson().toJson(respBean.getData())).startForResult(102);
                    }else{
                        Router.turnTo(mContext, CancellationNextActivity.class).startForResult(102);
                    }
                }, RxHelper.getHideProgressConsumer(this));
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case ConstantCodeUtils.LOGGED_OUT:
                setResult(ConstantCodeUtils.LOGGED_OUT);
                finish();
                break;
            default:
                break;
        }
    }
}
